﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
using Microsoft.WindowsMobile.Forms;
using System.Runtime.InteropServices;
namespace MapaMovil
{
    public partial class Camara : Form
    {
        public Camara()
        {
            InitializeComponent();
            System.Windows.Forms.MessageBox.Show("Pidieron foto a camara");
            CameraCaptureDialog cameraCaptureDialog = new CameraCaptureDialog();
            System.Windows.Forms.MessageBox.Show("Titulo");
            //string windowTitle = GetCameraCaptureDialogTitle(cameraCaptureDialog);
            string windowTitle = "nada";
            System.Windows.Forms.MessageBox.Show("Modo " + windowTitle);
            cameraCaptureDialog.Mode = CameraCaptureMode.Still;
            System.Windows.Forms.MessageBox.Show("Dialogo abre");
            DialogResult dr=DialogResult.Cancel;
            try
            {
                dr = cameraCaptureDialog.ShowDialog();
            }
            catch (Exception e){
                System.Windows.Forms.MessageBox.Show("Error:"+e.Message + "\n" + e.StackTrace);
            }
                System.Windows.Forms.MessageBox.Show("Resultado dialogo");
            if (dr == DialogResult.OK)
            {
                System.Windows.Forms.MessageBox.Show("Dialogo OK");
                string capturedFileName = cameraCaptureDialog.FileName;
                System.Windows.Forms.MessageBox.Show(capturedFileName);
            }
            System.Windows.Forms.MessageBox.Show("Dialogo cancel");
            System.Windows.Forms.MessageBox.Show(dr.ToString());
            cameraCaptureDialog.Dispose();
            IntPtr ptr = FindWindow(windowTitle);
            if (ptr != IntPtr.Zero)
            {
                System.Windows.Forms.MessageBox.Show(string.Format("Found window '{0}' with ptr={1} ", windowTitle, ptr));
                //force the camera closed
                DestroyWindow(ptr);
            }
        }
        private string GetCameraCaptureDialogTitle(CameraCaptureDialog dlg)
        {
            if (dlg.Mode == CameraCaptureMode.Still)
            {
                return string.Format("{0} [{1}]", dlg.Title, "Photo");
            }
            else
            {
                return string.Format("{0} [{1}]", dlg.Title, "Video");
            }
        }
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern IntPtr DestroyWindow(IntPtr hWnd);

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        public static IntPtr FindWindow(string windowTitle)
        {
            return FindWindow(null, windowTitle);
        }
    }
}